package main.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import main.dao.UserTableDB;
import main.model.User;
import main.view.Main;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ChangeUserInformation {

    private String value;

    @FXML
    private Button search;

    @FXML
    private TextField searchbar;

    @FXML
    private Button setbacks;

    @FXML
    private TableView<User> userTableView;

    @FXML
    private TableColumn<User,String> idcard;

    @FXML
    private TableColumn<User,String> name;

    @FXML
    private TableColumn<User,String> password;

    @FXML
    private TableColumn<User,String> grade;

    @FXML
    private TableColumn<User,String> college;

    @FXML
    private TableColumn<User,Object> operate;

    @FXML
    void setbacks(ActionEvent event) throws IOException {
        new Main().initAdminMenu();

    }

    @FXML
    void inputConditions(KeyEvent event) {

    }

    @FXML
    void submitSearch(ActionEvent event) throws SQLException {
        idcard.setCellValueFactory(new PropertyValueFactory<>("username"));
        password.setCellValueFactory(new PropertyValueFactory<>("password"));
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        grade.setCellValueFactory(new PropertyValueFactory<>("grade"));
        college.setCellValueFactory(new PropertyValueFactory<>("college"));
        operate.setCellValueFactory(new PropertyValueFactory<>("operate"));
        ObservableList<User> data = FXCollections.observableArrayList();
        this.value = this.searchbar.getText();
        ResultSet rst =  new UserTableDB().date(this.value);
        while(rst.next()) {
            Button edit = new Button("操作");
            data.add(new User(rst.getString(1),rst.getString(2),rst.getString(3),rst.getString(4),rst.getString(5),edit));
            userTableView.setItems(data);
        }
    }

}
